const Promise = require('./es6-promise.min.js')
const base = 'https://sp.scdyfn.com'
//const base = 'https://easy-mock.com'

/**
 * 添加finally
 */
Promise.prototype.finally = function (callback) {
    let P = this.constructor
    return this.then(
        value => P.resolve(callback()).then(() => value),
        reason => P.resolve(callback()).then(() => {
            throw reason
        })
    )
}

/**
 * 封装post请求
 */
const post = (url, data) => {

    return new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中..',
        })
        //网络请求
        wx.request({
            url: base + url,
            data: data,
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token'),
                'name': 'sp'
            },
            success: function (res) { //服务器返回数据
                wx.hideLoading()
                if (res.statusCode == 200 && res.data.status === 200) {
                    resolve(res.data)
                } else if (res.statusCode == 200 && res.data.status === 403) {
                    wx.showToast({
                        title: '登录失效~',
                    })

                } else { //返回错误提示信息
                    reject(res.data)
                }
            },
            error: function (e) {
                wx.hideLoading()
                reject('网络出错')
            }
        })
    })
}

/**
 * 封装get请求
 */
const get = (url, data) => {
    return new Promise((resolve, reject) => {
        //网络请求
        wx.request({
            url: base + url,
            data: data,
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token'),
                'name': 'sp'
            },
            success: function (res) { //服务器返回数据
                if (res.statusCode == 200 && res.data.status === 200) {
                    resolve(res);
                } else { //返回错误提示信息
                    reject(res.data);
                }
            },
            error: function (e) {
                reject('网络出错');
            }
        })
    })
}

/**
 * 微信登陆
 */
const wxLogin = (user, inviter = '') => {
    return new Promise((resolve, reject) => {
        wx.showLoading({
            title: '登录中..',
        })
        wx.login({
            success(res) {
                if (res.code) {
                    console.log(res.code)
                    // 发起网络请求
                    wx.request({
                        url: base + '/login/login',
                        data: {
                            code: res.code,
                            user,
                            inviter,
                        },
                        method: 'POST',
                        header: {
                            'content-type': 'application/json',
                            'token': wx.getStorageSync('token'),
                            'name': 'sp'
                        },
                        success: function (res) { //服务器返回数据
                            wx.hideLoading()
                            if (res.statusCode == 200 && res.data.status === 200) {

                                resolve(res.data.data)
                            } else { //返回错误提示信息
                                reject(res.data)
                            }
                        },
                        error: function (e) {
                            wx.hideLoading()
                            reject('网络出错')
                        }
                    })

                } else {
                    wx.hideLoading()
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    })
}

/**
 * 封装异步api
 */
const wxPromisify = fn => {
    return function (obj = {}) {
        return new Promise((resolve, reject) => {
            obj.success = function (res) {
                resolve(res)
            }

            obj.fail = function (res) {
                reject(res)
            }

            fn(obj)
        })
    }
}

/**
 * 微信api
 */
const getLocationPromisified = wxPromisify(wx.getLocation) //获取经纬度
const showModalPromisified = wxPromisify(wx.showModal) //弹窗

module.exports = {
    post,
    get,
    wxLogin,
    getLocationPromisified,
    showModalPromisified
}